package com.forward.mobile.optitrans.util;

import java.security.SecureRandom;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;

public class StringCryptor {

    private static final String CIPHER_ALGORITHM = "AES";
    private static final String RANDOM_GENERATOR_ALGORITHM = "SHA1PRNG";
    private static final int RANDOM_KEY_SIZE = 128;
    private static final String PASSWORD = "optitransforward";

    // Decrypts string encoded in Base64
    public static String decrypt(String encryptedData) throws Exception {

     

        SecretKeySpec secretKeySpec = new SecretKeySpec(PASSWORD.getBytes(), CIPHER_ALGORITHM);
        Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);
        cipher.init(Cipher.DECRYPT_MODE, secretKeySpec);

        byte[] encrypted = Base64Coder.decode(encryptedData);
        byte[] decrypted = cipher.doFinal(encrypted);

        return new String(decrypted);
    }

    public static String encrypt(String data) {
        try {
          
            byte[] clear = data.getBytes();
            SecretKeySpec secretKeySpec = new SecretKeySpec(PASSWORD.getBytes(), CIPHER_ALGORITHM);
            Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);
            cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec);

            byte[] encrypted = cipher.doFinal(clear);
            String encryptedString = new String(Base64Coder.encode(encrypted));

            return encryptedString;
        } catch (Exception exception) {
            exception.printStackTrace();
            return null;
        }
    }

}
